home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS04.ADF / C / WhichFont.c < prev   
C/C++ Source or Header  |  1985-11-03  |  11KB  |  278 lines

  1.  
  2. /* "whichfont.c" */
  3.  
  4. /* sample program that asks AvailFonts() to make a list of the fonts
  5.  * that are available and makes a list of them, then opens a separate
  6.  * window and prints a description of the various attributes that can
  7.  * be applied to the fonts, in the font itself.  Notice that not all
  8.  * fonts accept all attributes (garnet9 for example, won't underline)
  9.  *
  10.  * Also note, if you run this, that not all fonts are as easily readable
  11.  * in the various bold and italicized modes.... this rendering is done
  12.  * in a fixed manner by software and the fonts were not necessarily
  13.  * designed to accept it.  It is always best to have a font that has
  14.  * been designed with a bold or italic characteristic built-in rather
  15.  * than try to bold-ize or italicize and existing plain font.
  16.  */
  17.  
  18. /* Author:  Rob Peck  10/28/85  */
  19.  
  20. #define AFTABLESIZE 2000
  21.  
  22. #include <exec/types.h>
  23. #include <exec/memory.h>
  24. #include <libraries/dos.h>
  25. #include <graphics/text.h>
  26. #include <libraries/diskfont.h>
  27. #include <intuition/intuition.h>
  28.  
  29. struct AvailFonts *af;
  30. struct AvailFontsHeader *afh;
  31. extern int AvailFonts();
  32.  
  33. struct TextFont *tf;
  34. struct TextAttr ta;
  35.  
  36. ULONG DosBase;
  37. ULONG DiskfontBase;
  38. ULONG IntuitionBase;
  39. ULONG GfxBase;
  40.  
  41. struct NewWindow nw = {
  42.         10, 10,        /* starting position (left,top) */
  43.         620,40,        /* width, height */
  44.         -1,-1,          /* detailpen, blockpen */
  45.         0,              /* flags for idcmp */
  46.   WINDOWDEPTH|WINDOWSIZING|WINDOWDRAG|SIMPLE_REFRESH|ACTIVATE|GIMMEZEROZERO,
  47.                         /* window gadget flags */
  48.         0,              /* pointer to 1st user gadget */
  49.         NULL,           /* pointer to user check */
  50.         "Text Font Test", /* title */
  51.         NULL,           /* pointer to window screen */
  52.         NULL,           /* pointer to super bitmap */
  53.         100,45,         /* min width, height */
  54.         640,200,        /* max width, height */
  55.         WBENCHSCREEN};
  56.  
  57. struct Window *w;
  58. struct RastPort *rp;
  59.  
  60. SHORT text_styles[ ] = { FS_NORMAL, FSF_UNDERLINED, FSF_ITALIC, FSF_BOLD, 
  61.                         FSF_ITALIC | FSF_BOLD, FSF_BOLD | FSF_UNDERLINED,
  62.                         FSF_ITALIC | FSF_BOLD | FSF_UNDERLINED };
  63.                 
  64. char *text[ ] = { " Normal Text", " Underlined", " Italicized", " Bold", 
  65.                   " Bold Italics", " Bold Underlined", 
  66.                   " Bold Italic Underlined" };
  67. char textlength[ ] = { 12, 11, 11, 5, 13, 16, 23 };
  68.  
  69. char *pointsize[] = { " 0"," 1"," 2"," 3"," 4"," 5"," 6"," 7"," 8"," 9",
  70.                    "10","11","12","13","14","15","16","17","18","19",
  71.                    "20","21","22","23","24","25","26","27","28","29",
  72.                    "30","31"};
  73.  
  74. char fontname[40];
  75. char dummy[100];        /* provided for string length calculation */
  76. char outst[100];        /* build something to give to Text, see note in 
  77.                          * the program body about algorithmically
  78.                          * generated styles 
  79.                          */
  80.  
  81. main()
  82. {
  83.         UBYTE fonttypes;
  84.         int i,j,k,m;
  85.         SHORT afsize;
  86.         SHORT style;
  87.         SHORT sEnd;     /* numerical position of end of string terminator,
  88.                          * and coincidently the length of the string. */
  89.  
  90.         if( (DosBase = OpenLibrary("dos.library", 0)) == NULL) exit(-1);
  91.         if((DiskfontBase=OpenLibrary("diskfont.library",0))==NULL) exit(-4);
  92.         if((IntuitionBase=OpenLibrary("intuition.library",0))==NULL) exit(-2);
  93.         if((GfxBase=OpenLibrary("graphics.library",0))==NULL) exit(-3);
  94.         
  95.         tf=NULL;        /* no font currently selected */
  96.         afsize = AFTABLESIZE;   /* show how large a buffer is available */
  97.         fonttypes = 0xff;       /* show us all font types */
  98.         
  99.         afh = (struct AvailFontsHeader *) AllocMem(afsize, MEMF_CLEAR);
  100.         if(afh == NULL) exit(-5);
  101.         
  102.         printf("\nSearching for Fonts\n");
  103.         AvailFonts(afh, afsize, fonttypes);
  104.  
  105.         af = (struct AvailFonts *) &afh[1];  /* bypass header to get to the
  106.                                               * first of the availfonts */
  107.  
  108.         for (j = 0; j < afh->afh_NumEntries; j++)
  109.         {
  110.         if((af->af_Attr.ta_Flags & FPF_REMOVED) ||
  111.            (af->af_Attr.ta_Flags & FPF_REVPATH) ||
  112.                     ((af->af_Type&AFF_MEMORY)&&
  113.                         (af->af_Attr.ta_Flags&FPF_DISKFONT)))
  114.                         ;       /* do nothing if font is removed, or if
  115.                                  * font designed to be rendered rt->left
  116.                                  * (simple example writes left to right)
  117.                                  * or if font both on disk and in ram, 
  118.                                  * don't list it twice. */
  119.  
  120.         /* AvailFonts performs an AddFont to the system list;
  121.          * if run twice, you get two entries, one of "af_Type 1" saying
  122.          * that the font is memory resident, and the other of "af_Type 2"
  123.          * saying the font is disk-based.  The third part of the 
  124.          * if-statement lets you tell them apart if you are scanning
  125.          * the list for unique elements;  it says "if its in memory and
  126.          * it is from disk, then don't list it because you'll find another
  127.          * entry in the table that says it is not in memory, but is on disk.
  128.          * (Another task might have been using the font as well, creating
  129.          * the same effect).
  130.          */
  131.  
  132.         else
  133.            {
  134.                 printf("\nFont name found was: %ls",af->af_Attr.ta_Name);
  135.                 printf("  and its point size is: %ld",af->af_Attr.ta_YSize);
  136.                 /* Style parameter is in af->af_Attr.ta_Style,
  137.                  * Flags parameter is in af->af_Attr.ta_Flags.
  138.                  */             
  139.            }
  140.         af++;
  141.         }
  142.         /* now that we've listed the fonts, lets look at them */
  143.  
  144.         w = (struct Window *)OpenWindow(&nw);
  145.         rp = w->RPort;
  146.  
  147.     for(m=0; m<2; m++)  /* do normal video, then inverse video */
  148.      {
  149.  
  150.         af = (struct AvailFonts *)&afh[1]; /* reset value of af to original */
  151.         SetAPen(rp,1);          
  152.  
  153.         if(m == 0)SetDrMd(rp,JAM1);
  154.         else SetDrMd(rp,JAM1+INVERSVID);
  155.  
  156.         /* now print a line that says what font and what style it is */
  157.  
  158.         for (j=0; j < afh->afh_NumEntries; j++)
  159.         {
  160.         CStringAppend(&fontname[0],af->af_Attr.ta_Name);  
  161.                         /* copy name into build-name area */
  162.                         /* already has ".font" onto end of it */
  163.         
  164.         ta.ta_Name = &fontname[0];
  165.         ta.ta_YSize = af->af_Attr.ta_YSize;     /* ask for this size */
  166.         ta.ta_Style = af->af_Attr.ta_Style;     /* ask for designed style */
  167.         ta.ta_Flags = FPF_ROMFONT|FPF_DISKFONT|FPF_PROPORTIONAL|FPF_DESIGNED;
  168.                 /* accept it from anywhere it exists */
  169.         style = ta.ta_Style;
  170.  
  171.         if(!((af->af_Attr.ta_Flags & FPF_REMOVED) ||
  172.            (af->af_Attr.ta_Flags & FPF_REVPATH) ||
  173.            ((af->af_Type&AFF_MEMORY)&&
  174.            (af->af_Attr.ta_Flags&FPF_DISKFONT))))
  175.  
  176.            /* this is an IF-NOT, the reverse of the earlier if-test on
  177.             * these same parameters 
  178.             */
  179.            {
  180.                 tf = (struct TextFont *) OpenDiskFont(&ta);
  181.         
  182.                 if (tf != 0) 
  183.                 { 
  184.                 SetFont(w->RPort, tf);
  185.                 for(k=0; k<7; k++)
  186.                     {
  187.                     style = text_styles[k];
  188.                     SetSoftStyle(w->RPort,style,255);
  189.                     SetRast(rp,0);      /* erase any previous text */
  190.                     Move(rp,10,20);     /* move down a bit from the top */
  191.                     sEnd = CStringAppend(&outst[0],af->af_Attr.ta_Name);
  192.                     sEnd = sEnd + CStringAppend(&outst[sEnd],"  ");
  193.                     sEnd = sEnd + CStringAppend(&outst[sEnd],
  194.                                              pointsize[af->af_Attr.ta_YSize]);
  195.                     sEnd = sEnd + CStringAppend(&outst[sEnd]," Points, ");
  196.                     CStringAppend(&outst[sEnd],text[k]);
  197.                     Text(rp,&outst[0],CStringAppend(&dummy[0],&outst[0]));
  198.  
  199.                     /* Have to build the string before sending it out to
  200.                      * text IF ALGORITHMICALLY GENERATING THE STYLE since 
  201.                      * the kerning and spacing tables are based on the
  202.                      * vanilla text, and not the algorithmically generated
  203.                      * style.  If you send characters out individually,
  204.                      * it is possible that the enclosing rectangle of
  205.                      * a later character will chop off the trailing edge
  206.                      * of a preceding character 
  207.                      */
  208.  
  209.                     /* ************************************************** 
  210.                     This alternate method, when in INVERSVID, exhibits the
  211.                     problem described above.
  212.  
  213.                     Text(rp,af->af_Attr.ta_Name,STRLEN(af->af_Attr.ta_Name));
  214.                     Text(rp,"  ",2);
  215.                     Text(rp,pointsize[af->af_Attr.ta_YSize],2);
  216.                     Text(rp," Points, ",9);
  217.         
  218.                     Text(rp,text[k],textlength[k]);  
  219.                     **************************************************  */ 
  220.  
  221.                     Delay(40);  /* use the DOS time delay function 
  222.                                  * specifies 60ths of a second */
  223.                     }
  224.                 CloseFont(tf); /* close the old one */
  225.         
  226.        /* NOTE: 
  227.         *     Even though you close a font, it doesn't get unloaded
  228.         *     Memory unless a font with a different name is specified
  229.         *     for loading.  In this case, any font (except the topaz
  230.         *     set) which has been closed can have its memory area
  231.         *     freed and it will no longer be accessible.  If you close
  232.         *     a font to go to a different point-size, it will NOT cause
  233.         *     a disk-access.  
  234.         *
  235.         *  ALSO NOTE:   
  236.         *      Loading a font loads ALL of the point
  237.         *      sizes contained in that font's directory!!!!
  238.         */
  239.  
  240.             }   /* end of if-tf-ne-0 */
  241.           }     /* end of if-(in memory but from disk) */
  242.         af++;
  243.         }       /* Do next font now */
  244.      }          /* end of for-loop, controlled by m */
  245.  
  246.         FreeMem(afh,AFTABLESIZE);
  247.         CloseWindow(w);
  248.         CloseLibrary(IntuitionBase);   
  249.         CloseLibrary(DosBase);  
  250.         CloseLibrary(DiskfontBase);   
  251.         CloseLibrary(GfxBase);   
  252.  
  253. }
  254.  
  255. /* copy a string and return the number of characters added to 
  256.  * a string.  Effectively returns the length of the string if
  257.  * not adding anything */
  258.  
  259. int CStringAppend(dest, source) 
  260. char *dest;
  261. char *source;
  262. {
  263.     int i=0; 
  264.     char *s = source; 
  265.     char *d = dest; 
  266.     while (( i <79 )&&( *d = *s )) { d++; s++; i++; } 
  267.         /* if find a NULL in source, end the copy, but the NULL itself
  268.          * gets copied over to the destination.  If no NULL, then 79
  269.          * characters get copied, then a terminating NULL is added */
  270.     if(i < 79) return(i);
  271.     else {*d = 0; return(i);    }
  272.         /* value returned is the position of the terminating NULL
  273.          * to allow other strings to be appended simply using the
  274.          * next append command in sequence */
  275.  
  276. }
  277.  
  278.